package com.ibm.ibank.dubbo.gateway;

import com.ibm.ibank.dubbo.gateway.metadata.MetadataCollector;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.Map;


@ConfigurationProperties(prefix = "mapping")
public class Config {


    @Value("${proxy.registry.address}")
    private String registryAddress;

    @Value("${proxy.registry.group}")
    private String registryGroup;



    private Map<String, String> appPackageMap;

    public Map<String, String> getAppPackageMap() {
        return appPackageMap;
    }

    public void setAppPackageMap(Map<String, String> appPackageMap) {
        this.appPackageMap = appPackageMap;
    }

    @Bean
    Registry getRegistry() {
        URL url = URL.valueOf(registryAddress);
        if (StringUtils.isNotEmpty(registryGroup)) {
            url = url.addParameter(org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY, registryGroup);
        }
        RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
        Registry registry = registryFactory.getRegistry(url);
        return registry;
    }
}
